package com.example.memorypoint.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.Button;
import android.widget.TextView;

import com.example.memorypoint.R;
import com.example.memorypoint.adapter.IntroductoryAdapter;

import java.util.ArrayList;
import java.util.List;

public class IntroductoryActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    private Button mButton;
    private List<View> viewList;
    private TextView tx1;
    private TextView tx2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题栏
        setContentView(R.layout.activity_introductory);

        initView();  // 初始化页面视图
        initAdapter(); // 初始化适配器
        initStart();  // 初始化文本动画和按钮
    }

    // 设置TextView的淡入动画
    private void animateTextView(TextView textView, int delay) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "alpha", 0f, 1f);
        animator.setDuration(1000);
        animator.setStartDelay(delay);
        animator.setInterpolator(new AccelerateDecelerateInterpolator());
        animator.start();
    }

    /**
     * 设置引导页的textView文本的点击事件
     */
    private void initStart() {
        // 获取引导页的TextView和按钮
        tx1 = viewList.get(0).findViewById(R.id.tx1);
        tx2 = viewList.get(0).findViewById(R.id.tx2);
        // 设置动画效果
        animateTextView(tx1, 0);
        animateTextView(tx2, 0);

        mButton = viewList.get(0).findViewById(R.id.btn_open);
        mButton.setOnClickListener(view -> {
            startActivity(new Intent(IntroductoryActivity.this, MainActivity.class));
            IntroductoryActivity.this.finish(); // 关闭引导页Activity
        });
    }

    /**
     * 设置适配器
     */
    private void initAdapter() {
        IntroductoryAdapter adapter = new IntroductoryAdapter(viewList);
        mViewPager.setAdapter(adapter);
    }

    /**
     * 初始化viewPager和引导页
     */
    private void initView() {
        mViewPager = findViewById(R.id.introductory_viewPager);
        viewList = new ArrayList<>();
        // 暂时只添加一个引导页面
        viewList.add(getView(R.layout.introductory_a));  // 添加引导页布局
    }

    /**
     * 根据布局ID获取View对象
     */
    private View getView(int resId) {
        return LayoutInflater.from(this).inflate(resId, null);
    }
}
